package com.renrenyou.duanshipin.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;

import com.renrenyou.duanshipin.R;
import com.renrenyou.duanshipin.about.WebActivity;
import com.renrenyou.duanshipin.databinding.DialogPolicyBinding;
import com.renrenyou.duanshipin.net.Constance;

public class PolicyDialog extends AppCompatDialog {
    public PolicyDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DialogPolicyBinding binding = DialogPolicyBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.tvPolicyNegative.setOnClickListener(v -> {
            if (mCallback != null) {
                mCallback.onNegative();
            }
        });

        binding.tvPolicyPositive.setOnClickListener(v -> {
            if (mCallback != null) {
                mCallback.onPositive();
            }
        });

        SpannableString sb = new SpannableString("您必须同意相关隐私政策和用户协议");
        sb.setSpan(new ForegroundColorSpan(getContext().getColor(R.color.red)), 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(getContext().getColor(R.color.red)), sb.length() - 4, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        sb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                //隐私
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("title", "隐私政策");
                intent.putExtra("url", Constance.YINSI);
                getContext().startActivity(intent);
            }
        }, 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                //协议
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("title", "用户协议");
                intent.putExtra("url", Constance.XIEYI);
                getContext().startActivity(intent);
            }
        }, sb.length() - 4, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.tvPolicyContent.setText(sb);
        binding.tvPolicyContent.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private PositiveCallback mCallback;

    public void setOnPositiveListener(PositiveCallback listener) {
        this.mCallback = listener;
    }

    public interface PositiveCallback {
        void onPositive();

        void onNegative();
    }
}
